package com.ziq.zcamp.dialog.share

import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ziq.base.base.BaseDialog
import com.ziq.base.extension.*
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.product.PlaceProduct
import com.ziq.zcamp.databinding.DialogShareBinding
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.UserManager
import com.ziq.zcamp.manager.WXAccount
import com.ziq.zcamp.util.ImageUtil

class ProductShareDialog(val activity: AppCompatActivity, val tools: List<ActionItem> = mutableListOf()) : BaseDialog(activity) {

    private val binding: DialogShareBinding by binding()
    private lateinit var shareActionAdapter: ActionAdapter
    private lateinit var toolActionAdapter: ActionAdapter

    private val uidSuffix by lazy{
        val id = UserManager.instance.userInfo.value?.id.toString()
        if (id.isEmpty()){
            ""
        }else{
            "&shareuid=${id}"
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window?.decorView?.let {
            window?.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
        }

        binding.root
        binding.root.click {
            dismiss()
        }
        shareActionAdapter = ActionAdapter()
        binding.rvShareActionList.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        binding.rvShareActionList.adapter = shareActionAdapter
        val actions = mutableListOf<ActionItem>()
        if (WXAccount.instance.isWxInstalled()) {
            actions.add(
                ActionItem(
                    R.drawable.icon_share_wechat,
                    GlobalContextManager.getString(R.string.share_wechat)
                )
            )
        }

        shareActionAdapter.setData(actions)
        shareActionAdapter.setActionCallback {
            when (it) {
                GlobalContextManager.getString(R.string.share_wechat) -> {
                    if(placeProduct != null ){
                        val pic = getSharePictureForMiniProgram()
                        if(pic.isEmpty()){
                        }
                        ImageUtil.getBitmap(0, pic, 0, 0) { id, bitmap ->
                            dismiss()
                            WXAccount.instance.shareMiniProgram(bitmap, getShareTitleForMiniProgram(), path = getMiniProgramPath())
                        }
                    }

                }
            }
        }



        toolActionAdapter = ActionAdapter()
        binding.rvShareToolList.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        binding.rvShareToolList.adapter = toolActionAdapter
        toolActionAdapter.setData(tools)
        toolActionAdapter.setActionCallback {
            dismiss()
            actionCallBack?.invoke(it)
        }

        binding.btnCancel.click {
            dismiss()
        }
        setAnimateView(binding.root)
    }

    var placeProduct: PlaceProduct? = null

    fun getSharePictureForMiniProgram() : String{
        if(placeProduct != null ){
            if(placeProduct?.goods_url_list?.get(0)?.url?.isNotEmpty() == true){
                return placeProduct?.goods_url_list?.get(0)?.url?:""
            }
        }
        return ""
    }

    fun getShareTitleForMiniProgram() : String{
        if(placeProduct != null){
            return placeProduct?.name?:""
        }
        return ""
    }

    fun getMiniProgramPath() : String{
        if(placeProduct != null ){
            return "/subpages/camp/commodity-detail/index?id=${placeProduct?.commodity_id}${uidSuffix}"
        }
        return "/pages/community/index"
    }


    fun setPlaceProduct(data: PlaceProduct?) : ProductShareDialog {
        if(data != null){
            placeProduct = data
        }
        return this
    }

    var actionCallBack : ((action:String)->Unit)? = null

    fun setActionCallback(callback : (action:String)->Unit) : ProductShareDialog {
        actionCallBack = callback
        return this
    }

    override fun initHeight(): Int {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun initWidth(): Int {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun initGravity(): Int {
        return Gravity.BOTTOM or Gravity.CENTER
    }


}